package FilePanel;

import java.io.File;
import FilePanel.controls.ControlButton;
import FilePanel.controls.ControlChooser;
import FilePanel.controls.ControlField;
import FilePanel.controls.ControlList;

/**
 * @author      Erwan Osouf, Lucas Ennouchi
 * @version     1.0                                
 * @since       29/01/2012
 */

public class FilePanelImp implements FilePanel {
	
	private FilePanelModel model;
	private FilePanelUI view;
	private FilePanelControl controller;
	
	/**
	 * FilePanelImp constructor to assemble everything
	 * @param model
	 * @param view
	 * @param controller
	 */
	public FilePanelImp(FilePanelModel model, FilePanelUI view,
			FilePanelControl controller) {
		super();
		this.model = model;
		this.view = view;
		this.controller = controller;
	}
	
	
	public FilePanelImp(){
		FilePanelModel model = new FilePanelModel(new File("/"));
		FilePanelUI view = new FilePanelUI();
		ControlButton cb = new ControlButton(view.getFileSelectionButton());
		ControlChooser cc = new ControlChooser(view.getFileChooser());
		ControlField cf = new ControlField(view.getFileSelectionField());
		ControlList cl = new ControlList(view.getFileList());
		FilePanelControl controller = new FilePanelControl(model, view, cb, cc, cf, cl);
		
		// Chooser
		cc.setSuperController(controller);
		
		//Button
		cb.setSuperController(controller);
		view.getFileSelectionButton().addActionListener(cb);
		
		//Field
		cf.setSuperController(controller);
		view.getFileSelectionField().addCaretListener(cf);
		model.addObserver(cf);
		
		//List
		cl.setSuperController(controller);
		view.getFileList().addListSelectionListener(cl);
		model.addObserver(cl);
		
		//ButtonChooser 
		view.getFileChooserButton().addActionListener(cc);
		
		this.model = model;
		this.view = view;
		this.controller = controller;
		model.addObserver(controller);
	}

	

	public FilePanelModel getModel() {
		return model;
	}

	public FilePanelUI getView() {
		return view;
	}

	public FilePanelControl getController() {
		return controller;
	}

	@Override
	public void updateDisplay() {
		this.view.repaint();
		this.view.validate();
		
	}

	@Override
	public Data getData() {
		return model;
	}
}
